package com.sgb.kjwl.viewmodel.self.packers;

import android.text.TextUtils;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.SelfEmploymentApplyEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfEmploymentApplyActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfEmploymentApplyModel extends BaseViewModel {

    private SelfEmploymentApplyActivity activity;
    private int page = 1;
    private int limit = 10;

    public SelfEmploymentApplyModel(SelfEmploymentApplyActivity activity, SelfRepertory selfRepertory) {
        this.activity = activity;
    }

    public void getEmploymentApplyListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        Map map = new HashMap();
        map.put("page", page);
        map.put("limit", limit);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        int userType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else if (userType == 2){
            map.put("teamNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, ""));
        }else {
            map.put("companyNo", companyNo);
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getEmploymentApply(map)
                .enqueue(new Callback<BaseEntity<List<SelfEmploymentApplyEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<SelfEmploymentApplyEntity>>> call, Response<BaseEntity<List<SelfEmploymentApplyEntity>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            if (response.body().getData() != null && response.body().getData().size() >= limit) {
                                activity.showList(response.body().getData(), isLoadMore, false);
                            } else {
                                activity.showList(response.body().getData(), isLoadMore, true);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<SelfEmploymentApplyEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                    }
                });
    }

    public void getH5Url(String no){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("workforcedetailSingle").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no;
                    WebViewX5HostActivity.start(activity, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void deleteEmploymentApply(String no){
        Map map = new HashMap();
        map.put("no", no);
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().deleteEmploymentApply(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    getEmploymentApplyListModel(false);
                } else {
                    MToast.showToast(activity, response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
